export const initMenu = [
  {
    router: "/project",
    name: "项目",
    auth: ["PROJECT"],
  },
  {
    router: "/approval",
    name: "审核",
    auth: ["AUDIT"],
  },
  {
    router: "/indicators",
    name: "指标库",
    auth: ["INDICATOR"],
  },
  {
    router: "/backend",
    name: "后台管理",
    auth: ["ADMIN"],
  },
];
/**
 * @description: 把映射转为选项数据
 * @param {Record<string, string>} obj 映射
 * @param {boolean} keyIsNumber 映射的key即选项的value是否为数字
 * @return {value:string|number;label:string;} value-label选项数据
 */
export function obj2list(obj: Record<string, string>, keyIsNumber?: boolean) {
  return Object.keys(obj).map((key) => ({
    value: keyIsNumber ? Number(key) : key,
    label: obj[key],
  }));
}
export function arr2option(arr: string[]) {
  return arr.map((item) => ({ label: item, value: item }));
}
/**
 * @description: 直接传入下载链接下载
 * @param {string} url 下载链接
 * @param {string} fileName 文件名
 */
export const downloadByUrl = (url: string, fileName: string) => {
  let a = document.createElement("a");
  a.href = url;
  a.download = fileName;
  a.click();
};
/**
 * @description: 传入blob数据，函数内生成下载链接下载
 * @param {Blob} blob 文件数据
 * @param {string} fileName 文件名
 */
export const downloadByBlob = (blob: Blob, fileName: string) => {
  let url = window.URL.createObjectURL(blob);
  downloadByUrl(url, fileName);
  window.URL.revokeObjectURL(url);
};
/**
 * @description: 将file对象转为base64
 * @param {File} file
 */
export function getBase64(file: File) {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    reader.onload = () => resolve(reader.result);
    reader.onerror = (error) => reject(error);
  });
}
export const defaultFormConfig = [
  {
    type: "table",
    label: "数据分解",
    key: "data",
    props: {
      rowKey: "id",
      canAdd: true,
      columns: [
        {
          title: "数据名称（单位）",
          dataIndex: "name",
          type: "input",
          width: 130,
          ellipsis: true,
          resizable: true,
        },
        {
          title: "数量",
          dataIndex: "count",
          type: "input",
          width: 130,
          ellipsis: true,
          resizable: true,
        },
        {
          title: "收集方法",
          dataIndex: "method",
          type: "input",
          width: 130,
          ellipsis: true,
          resizable: true,
        },
        {
          title: "数据年份",
          dataIndex: "year",
          type: "input",
          width: 130,
          ellipsis: true,
          resizable: true,
        },
        {
          title: "数据来源",
          dataIndex: "resource",
          type: "input",
          width: 130,
          ellipsis: true,
          resizable: true,
        },
        {
          title: "责任单位",
          dataIndex: "company",
          type: "input",
          width: 130,
          ellipsis: true,
          resizable: true,
        },
      ],
      dataSource: [{ id: new Date().valueOf() }],
    },
  },
  {
    type: "table", // 表格输入框，可以增一行填写
    label: "证明文件说明",
    key: "certification",
    props: {
      rowKey: "id",
      canAdd: false,
      showHeader: false,
      bordered: true,
      columns: [
        {
          title: "申报人",
          dataIndex: "_applicant",
          type: "text",
          width: 130,
          ellipsis: true,
          resizable: true,
          props: {
            style: {
              fontWeight: "bold",
            },
          },
        },
        {
          title: "申报人",
          dataIndex: "applicant",
          type: "input",
          width: 130,
          ellipsis: true,
          resizable: true,
        },
        {
          title: "单位",
          dataIndex: "_company",
          type: "text",
          width: 130,
          ellipsis: true,
          resizable: true,
          props: {
            style: {
              fontWeight: "bold",
            },
          },
        },
        {
          title: "单位",
          dataIndex: "company",
          type: "input",
          width: 130,
          ellipsis: true,
          resizable: true,
        },
        {
          title: "邮箱",
          dataIndex: "_email",
          type: "text",
          width: 130,
          ellipsis: true,
          resizable: true,
          props: {
            style: {
              fontWeight: "bold",
            },
          },
        },
        {
          title: "邮箱",
          dataIndex: "email",
          type: "input",
          width: 130,
          ellipsis: true,
          resizable: true,
        },
      ],
      dataSource: [
        {
          id: new Date().valueOf(),
          _applicant: "申报人",
          _company: "单位",
          _email: "邮箱",
        },
      ],
      summary: [
        [
          {
            dataIndex: "explanation",
            type: "textarea",
            props: {
              placeholder: "填入说明，不多余500字",
              showCount: true,
              maxlength: 500,
            },
            cellProps: {
              colSpan: 6,
            },
          },
        ],
      ],
      summaryData: {},
    },
  },
  {
    type: "table", // 表格输入框，可以增一行填写
    label: "证明文件提交",
    key: "certificationSubmit",
    props: {
      rowKey: "id",
      canAdd: true,
      columns: [
        {
          title: "文件名称",
          dataIndex: "name",
          type: "input",
          width: 130,
          ellipsis: true,
          resizable: true,
        },
        {
          title: "发行年份",
          dataIndex: "count",
          type: "input",
          width: 130,
          ellipsis: true,
          resizable: true,
        },
        {
          title: "来源单位",
          dataIndex: "method",
          type: "input",
          width: 130,
          ellipsis: true,
          resizable: true,
        },
        {
          title: "链接",
          dataIndex: "year",
          type: "input",
          width: 130,
          ellipsis: true,
          resizable: true,
        },
        {
          title: "附件",
          dataIndex: "resource",
          type: "upload",
          ellipsis: true,
          props: {
            buttonText: "上传",
            maxCount: 1,
          },
        },
      ],
      dataSource: [{ id: new Date().valueOf() }],
    },
  },
];
// 审核状态
export const enum ApprovalStatusEnum {
  Wait = 0,
  Success = 1,
  Fail = -1,
}
export const approvalStatusMap: Record<
  string,
  { text: string; color: string }
> = {
  [ApprovalStatusEnum.Wait]: {
    text: "待评审",
    color: "green",
  },
  [ApprovalStatusEnum.Success]: {
    text: "评审通过",
    color: "red",
  },
  [ApprovalStatusEnum.Fail]: {
    text: "评审失败",
    color: "grey",
  },
};
export const approvalStatusOptions = Object.keys(approvalStatusMap).map(
  (key) => ({
    value: approvalStatusMap[key]?.text,
    label: approvalStatusMap[key]?.text,
  })
);
// 项目规模
export const enum ScaleEnum {
  City = "市",
  District = "区",
  Street = "街道",
  Community = "社区",
}
export const scaleMap = {
  [ScaleEnum.City]: "市",
  [ScaleEnum.District]: "区",
  [ScaleEnum.Street]: "街道",
  [ScaleEnum.Community]: "社区",
};
export const scaleOptions = obj2list(scaleMap, false);
// 项目类型
export const enum ProjectTypeEnum {
  City = "城市",
  Business = "商业区",
  Insductry = "工业园区",
  Community = "社区",
  Usual = "通用",
}
export const projectTypeMap = {
  [ProjectTypeEnum.City]: "城市",
  [ProjectTypeEnum.Business]: "商业区",
  [ProjectTypeEnum.Insductry]: "工业园区",
  [ProjectTypeEnum.Community]: "社区",
  [ProjectTypeEnum.Usual]: "通用",
};
export const projectTypeOptions = obj2list(projectTypeMap, false);
// 资产组合
export const enum PortfolioEnum {
  Society = "社会项目",
  Green = "绿色项目",
  Climate = "气候项目",
}
export const portfolioMap = {
  [PortfolioEnum.Society]: "社会项目",
  [PortfolioEnum.Green]: "绿色项目",
  [PortfolioEnum.Climate]: "气候项目",
};
export const portfolioOptions = obj2list(portfolioMap, false);
// 适用阶段
export const enum ApplicableStageEnum {
  Baseline = "基线调研",
  Green = "绿色策略与设计",
  Financing = "融资策略、投资组合",
  Construct = "施工",
  Monitor = "运营和监测",
}
export const applicableStageMap = {
  [ApplicableStageEnum.Baseline]: "基线调研",
  [ApplicableStageEnum.Green]: "绿色策略与设计",
  [ApplicableStageEnum.Financing]: "融资策略、投资组合",
  [ApplicableStageEnum.Construct]: "施工",
  [ApplicableStageEnum.Monitor]: "运营和监测",
};
export const applicableStageOptions = obj2list(applicableStageMap, false);
// 关联领域
export const enum ConnectedDomainEnum {
  Energy = "能源",
  Water = "水",
  SolidWaste = "固废",
  Ductility = "韧性",
  Virescence = "绿化",
}
export const connectedDomainMap = {
  [ConnectedDomainEnum.Energy]: "能源",
  [ConnectedDomainEnum.Water]: "水",
  [ConnectedDomainEnum.SolidWaste]: "固废",
  [ConnectedDomainEnum.Ductility]: "韧性",
  [ConnectedDomainEnum.Virescence]: "绿化",
};
export const connectedDomainOptions = obj2list(connectedDomainMap, false);
export const enum IndicatorStatusEnum {
  Filled,
  Empty,
  Approve,
  Reject,
}
export const indicatorStatusMap = {
  [IndicatorStatusEnum.Filled]: "已填报",
  [IndicatorStatusEnum.Empty]: "未填报",
  [IndicatorStatusEnum.Approve]: "评审通过",
  [IndicatorStatusEnum.Reject]: "评审未通过",
};
export const indicatorStatusOptions = obj2list(indicatorStatusMap, true);
export const projectPrototypeForm: FormStaticItem[] = [
  {
    label: "项目规模",
    key: "projectScale",
    map: scaleMap,
    type: "select",
    props: {
      options: scaleOptions,
    },
  },
  {
    label: "项目类型",
    key: "projectType",
    map: projectTypeMap,
    type: "select",
    props: {
      options: projectTypeOptions,
    },
  },
  {
    label: "资产组合",
    key: "portfolio",
    map: portfolioMap,
    type: "select",
    props: {
      options: portfolioOptions,
    },
  },
  {
    label: "适用阶段",
    key: "applicableStage",
    map: applicableStageMap,
    type: "select",
    props: {
      options: applicableStageOptions,
    },
  },
  {
    label: "关联领域",
    key: "connectedDomain",
    map: connectedDomainMap,
    type: "select",
    props: {
      options: connectedDomainOptions,
    },
  },
];
export const enum IndicatorTypeEnum {
  Required = "强制项",
  Option = "优选项",
  Innovation = "创新项",
}
export const indicatorTypeMap: Record<IndicatorTypeEnum, any> = {
  [IndicatorTypeEnum.Required]: "强制项",
  [IndicatorTypeEnum.Option]: "优选项",
  [IndicatorTypeEnum.Innovation]: "创新项",
};
// 注册-申请目的
const enum ApplyPurposeEnum {
  Apply = "项目申报",
  Review = "项目审核",
  Indicator = "指标管理",
}
export const applyPurposeMap: Record<ApplyPurposeEnum | number, string> = {
  [ApplyPurposeEnum.Apply]: "项目申报",
  [ApplyPurposeEnum.Review]: "项目审核",
  [ApplyPurposeEnum.Indicator]: "指标",
};
export const applyPurposeOptions = obj2list(applyPurposeMap, false);
// 认证状态
export const enum UserIdentifyStatusEnum {
  Wait = 0,
  Success = 1,
  Fail = -1,
}
export const userIdentifyStatusMap: Record<
  UserIdentifyStatusEnum | number,
  string
> = {
  [UserIdentifyStatusEnum.Wait]: "待认证",
  [UserIdentifyStatusEnum.Success]: "认证成功",
  [UserIdentifyStatusEnum.Fail]: "认证失败",
};
export const userIdentifyStatusOptions = Object.keys(userIdentifyStatusMap).map(
  (key) => ({
    value: userIdentifyStatusMap[+key],
    label: userIdentifyStatusMap[+key],
  })
);
// 单位类型
export const companyTypeOptions = arr2option([
  "省市级政府",
  "区级政府/管委会",
  "设计机构",
  "咨询机构",
  "施工机构",
  "物业机构",
  "房地产开发商",
  "专业协会和行业协会",
  "科研机构",
  "金融机构",
  "非盈利环保组织",
  "公共事业/能源服务公司",
  "其他",
]);
export const companyTypeMap = companyTypeOptions.reduce<Record<string, string>>(
  (obj, item) => {
    obj[item.value] = item.label;
    return obj;
  },
  {}
);
// 专业领域
export const professionalFieldOptions = arr2option([
  "建筑设计领域",
  "工程技术领域",
  "化学领域",
  "环保领域",
  "能源领域",
  "建筑管理领域",
  "法律领域",
  "经济学领域",
  "教育领域",
  "市场营销领域",
  "政策研究领域",
  "社会组织领域",
  "学术领域",
  "环境艺术领域",
  "运营管理领域",
  "信息技术领域",
  "社会科学领域",
]);
export const professionalFieldMap = professionalFieldOptions.reduce<
  Record<string, string>
>((obj, item) => {
  obj[item.value] = item.label;
  return obj;
}, {});
// 评审结果
const enum ApprovalResultEnum {
  Success = 1,
  Modify = 2,
  Fail = -1,
}
export const approvalResultMap = {
  [ApprovalResultEnum.Success]: "达标",
  [ApprovalResultEnum.Modify]: "修订",
  [ApprovalResultEnum.Fail]: "不达标",
};
export const approvalResultOptions = obj2list(approvalResultMap, true);
